package framework.editor;


import framework.data.ReservaQuarto;
import framework.data.Pessoa;
import framework.db.PessoaManager;
import framework.panes.Editor;
import framework.panes.EditorPane;
import framework.db.PessoaManager;
import framework.panes.Editor;
import framework.panes.EditorPane;
import java.awt.Frame;
import java.sql.Date;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ReservaQuartoPane.java
 *
 * Created on Jun 16, 2011, 7:41:34 PM
 */
/**
 *
 * @author Thairony
 */
public class ReservaQuartoEditor extends EditorPane
{

   private ReservaQuarto reservaquarto;

    /**
     * 
     * @param Parent
     * @param reserva quarto
     * @return 
     */
    public static boolean edit( Frame parent, ReservaQuarto reservaquarto )
    {
        ReservaQuartoEditor pane = new ReservaQuartoEditor();
        
        pane.setReservaQuarto( reservaquarto );
        
        return Editor.edit( parent, pane, "Reserva de Quartos" );
    }
    
    /** Creates new form ReservaQuartoPane */
    public ReservaQuartoEditor()
    {
        initComponents ();
    }
    
    /**
     * 
     * @param rq 
     */
    private void setReservaQuarto( ReservaQuarto rq )
    {
        this.reservaquarto = rq;
        preencherCampos();
    }
    
    /**
     * 
     */
    private void preencherCampos()
    {
        tfQuarto.setText( String.valueOf( reservaquarto.getQuarto() ) );
        tfCadastro.setText( String.valueOf( reservaquarto.getCadastro() ) );
        tfValor.setText( String.valueOf( reservaquarto.getValor() ) );
        tfExtra.setText( String.valueOf( reservaquarto.getExtra() ) );
        tfEntrada.setText( String.valueOf( reservaquarto.getCheckIn() ) );
        tfSaida.setText( String.valueOf( reservaquarto.getCheckOut() ) );
    }
    
    /**
     * 
     */
    @Override
    public void accept()
    {
        reservaquarto.setQuarto( Integer.parseInt( tfQuarto.getText() ) );
        reservaquarto.setCadastro( Integer.parseInt( tfCadastro.getText() ) );
        reservaquarto.setValor( Double.parseDouble( tfValor.getText() ) );
        reservaquarto.setExtra( Double.parseDouble( tfExtra.getText() ) );
        reservaquarto.setCheckIn( Date.valueOf( tfEntrada.getText() ) );
        reservaquarto.setCheckOut( Date.valueOf( tfSaida.getText() ) );
    }

    @Override
    public String validateInput()
    {
        String erro = "";
        if ( tfDescQuarto.getText().isEmpty())
        {
            erro = " - Quarto inválido.";
        }
        if ( tfNomeCadastro.getText().isEmpty())
        {
            erro += "\n - Cadastro inválido.";
        }
        try
        {
            if ( Double.parseDouble( tfValor.getText() ) <= 0.0 )
            {
                erro += "\n - O valor deve ser maior do que 0 (zero).";
            }
        }
        catch ( Exception e ) { }
        
        return erro;
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        tfQuarto = new javax.swing.JTextField();
        tfDescQuarto = new javax.swing.JTextField();
        jPanel6 = new javax.swing.JPanel();
        tfCadastro = new javax.swing.JTextField();
        tfNomeCadastro = new javax.swing.JTextField();
        tfValor = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        tfEntrada = new javax.swing.JTextField();
        tfSaida = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        tfExtra = new javax.swing.JTextField();

        setLayout(new java.awt.GridBagLayout());

        jLabel1.setText("Quarto:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(15, 15, 5, 5);
        add(jLabel1, gridBagConstraints);

        jLabel2.setText("Cadastro:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 15, 5, 5);
        add(jLabel2, gridBagConstraints);

        jLabel3.setText("Valor:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 15, 5, 5);
        add(jLabel3, gridBagConstraints);

        jPanel5.setLayout(new java.awt.GridBagLayout());

        tfQuarto.setPreferredSize(new java.awt.Dimension(60, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(15, 0, 5, 5);
        jPanel5.add(tfQuarto, gridBagConstraints);

        tfDescQuarto.setEditable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(15, 0, 5, 15);
        jPanel5.add(tfDescQuarto, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        add(jPanel5, gridBagConstraints);

        jPanel6.setLayout(new java.awt.GridBagLayout());

        tfCadastro.setPreferredSize(new java.awt.Dimension(60, 20));
        tfCadastro.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfCadastroFocusLost(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 5);
        jPanel6.add(tfCadastro, gridBagConstraints);

        tfNomeCadastro.setEditable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 15);
        jPanel6.add(tfNomeCadastro, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        add(jPanel6, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 5);
        add(tfValor, gridBagConstraints);

        jLabel4.setText("Data Entrada:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 15, 15, 5);
        add(jLabel4, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 15, 5);
        add(tfEntrada, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 15, 15);
        add(tfSaida, gridBagConstraints);

        jLabel5.setText("Data Saída:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 15, 5);
        add(jLabel5, gridBagConstraints);

        jLabel6.setText("Extras:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 5);
        add(jLabel6, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 15);
        add(tfExtra, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void tfCadastroFocusLost(java.awt.event.FocusEvent evt)//GEN-FIRST:event_tfCadastroFocusLost
    {//GEN-HEADEREND:event_tfCadastroFocusLost
        try
        {
            Pessoa p = PessoaManager.getInstance().getPessoa( Integer.parseInt( tfCadastro.getText() ) );
            if (p != null) 
            {
                tfNomeCadastro.setText( p.getNome() );
            }
        }
        catch ( Exception e ) { }
        
    }//GEN-LAST:event_tfCadastroFocusLost

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JTextField tfCadastro;
    private javax.swing.JTextField tfDescQuarto;
    private javax.swing.JTextField tfEntrada;
    private javax.swing.JTextField tfExtra;
    private javax.swing.JTextField tfNomeCadastro;
    private javax.swing.JTextField tfQuarto;
    private javax.swing.JTextField tfSaida;
    private javax.swing.JTextField tfValor;
    // End of variables declaration//GEN-END:variables
}
